home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ASTRONOM / H139.ZIP / UI101.ZIP / IO / KB / KB_IBMPC.C < prev    next >
C/C++ Source or Header  |  1991-11-04  |  7KB  |  315 lines

  1. /*************************************************************************
  2.  
  3.     kb_ibmpc.c    Keyboard (KB) Subroutines
  4.             for Bywater Software
  5.  
  6.             Implementation for the IBM PC and Compatibles
  7.             utilizing BIOS keyboard routines.
  8.  
  9.             This implementation contains code specific
  10.             to the Microsoft Quick C (tm) compiler.
  11.  
  12.             Copyright (c) 1991, Ted A. Campbell
  13.  
  14.             Bywater Software
  15.             P. O. Box 4023 
  16.             Duke Station 
  17.             Durham, NC  27706
  18.  
  19.             email: tcamp@hercules.acpub.duke.edu
  20.  
  21.     Copyright and Permissions Information:
  22.  
  23.     All U.S. and international copyrights are claimed by the
  24.     author. The author grants permission to use this code
  25.     and software based on it under the following conditions:
  26.     (a) in general, the code and software based upon it may be 
  27.     used by individuals and by non-profit organizations; (b) it
  28.     may also be utilized by governmental agencies in any country,
  29.     with the exception of military agencies; (c) the code and/or
  30.     software based upon it may not be sold for a profit without
  31.     an explicit and specific permission from the author, except
  32.     that a minimal fee may be charged for media on which it is
  33.     copied, and for copying and handling; (d) the code must be 
  34.     distributed in the form in which it has been released by the
  35.     author; and (e) the code and software based upon it may not 
  36.     be used for illegal activities. 
  37.  
  38. **************************************************************************/
  39.  
  40. #include "stdio.h"
  41. #include "kb.h"
  42. #include "bios.h"
  43.  
  44. #ifndef TRUE
  45. #define TRUE    1
  46. #define FALSE   0
  47. #endif
  48.  
  49. #ifndef CR
  50. #define CR      0x0d
  51. #endif
  52.  
  53. /*************************************************************************
  54.  
  55.     FUNCTION:       kb_init()
  56.  
  57.     DESCRIPTION:    This function should perform any initialization
  58.             necessary for the keyboard system.
  59.  
  60.     INPUT:          none.
  61.  
  62.     RETURNS:        none.
  63.  
  64. **************************************************************************/
  65.  
  66. kb_init()
  67.     {
  68.     }
  69.  
  70. /*************************************************************************
  71.  
  72.     FUNCTION:       kb_deinit()
  73.  
  74.     DESCRIPTION:    This function should perform any necessary
  75.             deinitialization, that is, return the keyboard
  76.             to its default state when a Simple Software
  77.             program is to be exited.
  78.  
  79.     INPUT:          none.
  80.  
  81.     RETURNS:        none.
  82.  
  83. **************************************************************************/
  84.  
  85. kb_deinit()
  86.     {
  87.     }
  88.  
  89. /*************************************************************************
  90.  
  91.     FUNCTION:       kb_rxstat()
  92.  
  93.     DESCRIPTION:    This function determines whether a character is
  94.             ready from the console.  The function is used
  95.             especially in telecommunications programs,
  96.             where it is necessary to poll the keyboard
  97.             without locking up the program waiting for a
  98.             response.
  99.  
  100.     INPUT:          none.
  101.  
  102.     RETURNS:        The function returns 0 if no character is
  103.             available and 1 if a character is available.
  104.  
  105. **************************************************************************/
  106.  
  107. kb_rxstat()
  108.     {
  109.     if ( _bios_keybrd( _KEYBRD_READY ) == 0 )
  110.         {
  111.         return FALSE;
  112.         }
  113.     else
  114.         {
  115.         return TRUE;
  116.         }
  117.     }
  118.  
  119. /*************************************************************************
  120.  
  121.     FUNCTION:       kb_rx()
  122.  
  123.     DESCRIPTION:    This function returns a single character from
  124.             the keyboard.  If a character is not available
  125.             it waits.  The function should be able to
  126.             recognize any special keys, and return the
  127.             appropriate Simple Software KB conventions
  128.             designated for them.
  129.  
  130.     INPUT:          none.
  131.  
  132.     RETURNS:        The function returns the ASCII code for the
  133.             key pressed, or the Simple Software KB convention
  134.             (see kb.h) for a function or other special key.
  135.  
  136. **************************************************************************/
  137.  
  138. kb_rx()
  139.      {
  140.      unsigned int ax;
  141.      unsigned char ah, al;
  142.  
  143.      while( kb_rxstat() == 0 )
  144.       {
  145.       }
  146.  
  147.      ax = _bios_keybrd( _KEYBRD_READ );
  148.      ah = ax / 256;
  149.      al = ax % 256;
  150.  
  151.      if ( al != 0 )
  152.       {
  153.       return al;
  154.       }
  155.  
  156.      else
  157.       {
  158.       switch( ah )
  159.         {
  160.         case 0x50:
  161.             return KB_DOWN;
  162.             break;
  163.         case 0x48:
  164.             return KB_UP;
  165.             break;
  166.         case 0x4b:
  167.             return KB_LEFT;
  168.             break;
  169.         case 0x4d:
  170.             return KB_RIGHT;
  171.             break;
  172.         case 0x49:
  173.             return KB_P_UP;
  174.             break;
  175.         case 0x51:
  176.             return KB_P_DOWN;
  177.             break;
  178.         case 0x53:
  179.             return KB_DELETE;
  180.             break;
  181.         case 0x52:
  182.             return KB_INSERT;
  183.             break;
  184.         case 0x4f:
  185.             return KB_END;
  186.             break;
  187.         case 0x47:
  188.             return KB_HOME;
  189.             break;
  190.         case 0x3b:                      /* FK 1         */
  191.         case 0x54:
  192.             return KB_FK1;
  193.             break;
  194.         case 0x3c:                      /* FK 2         */
  195.         case 0x55:
  196.             return KB_FK2;
  197.             break;
  198.         case 0x3d:                      /* FK 3         */
  199.         case 0x56:
  200.             return KB_FK3;
  201.             break;
  202.         case 0x3e:                      /* FK 4         */
  203.         case 0x57:
  204.             return KB_FK4;
  205.             break;
  206.         case 0x3f:                      /* FK 5         */
  207.         case 0x58:
  208.             return KB_FK5;
  209.             break;
  210.         case 0x40:                      /* FK 6         */
  211.         case 0x59:
  212.             return KB_FK6;
  213.             break;
  214.         case 0x41:                      /* FK 7         */
  215.         case 0x5a:
  216.             return KB_FK7;
  217.             break;
  218.         case 0x42:                      /* FK 8         */
  219.         case 0x5b:
  220.             return KB_FK8;
  221.             break;
  222.         case 0x43:                      /* FK 9         */
  223.         case 0x5c:
  224.             return KB_FK9;
  225.             break;
  226.         case 0x44:                      /* FK 10        */
  227.         case 0x5d:
  228.             return KB_FK0;
  229.             break;
  230.         case 0x10:
  231.             return KB_ALT + 'Q';
  232.             break;
  233.         case 0x11:
  234.             return KB_ALT + 'W';
  235.             break;
  236.         case 0x12:
  237.             return KB_ALT + 'E';
  238.             break;
  239.         case 0x13:
  240.             return KB_ALT + 'R';
  241.             break;
  242.         case 0x14:
  243.             return KB_ALT + 'T';
  244.             break;
  245.         case 0x15:
  246.             return KB_ALT + 'Y';
  247.             break;
  248.         case 0x16:
  249.             return KB_ALT + 'U';
  250.             break;
  251.         case 0x17:
  252.             return KB_ALT + 'I';
  253.             break;
  254.         case 0x18:
  255.             return KB_ALT + 'O';
  256.             break;
  257.         case 0x19:
  258.             return KB_ALT + 'P';
  259.             break;
  260.         case 0x1E:
  261.             return KB_ALT + 'A';
  262.             break;
  263.         case 0x1F:
  264.             return KB_ALT + 'S';
  265.             break;
  266.         case 0x20:
  267.             return KB_ALT + 'D';
  268.             break;
  269.         case 0x21:
  270.             return KB_ALT + 'F';
  271.             break;
  272.         case 0x22:
  273.             return KB_ALT + 'G';
  274.             break;
  275.         case 0x23:
  276.             return KB_ALT + 'H';
  277.             break;
  278.         case 0x24:
  279.             return KB_ALT + 'J';
  280.             break;
  281.         case 0x25:
  282.             return KB_ALT + 'K';
  283.             break;
  284.         case 0x26:
  285.             return KB_ALT + 'L';
  286.             break;
  287.         case 0x2C:
  288.             return KB_ALT + 'Z';
  289.             break;
  290.         case 0x2D:
  291.             return KB_ALT + 'X';
  292.             break;
  293.         case 0x2E:
  294.             return KB_ALT + 'C';
  295.             break;
  296.         case 0x2F:
  297.             return KB_ALT + 'V';
  298.             break;
  299.         case 0x30:
  300.             return KB_ALT + 'B';
  301.             break;
  302.         case 0x31:
  303.             return KB_ALT + 'N';
  304.             break;
  305.         case 0x32:
  306.             return KB_ALT + 'M';
  307.             break;
  308.         default:
  309.             return 0;
  310.             break;
  311.         }
  312.       }
  313.      }
  314.  
  315.